<template>
  <div>
    <h1>首页</h1>

    <!-- 是否登录 -->
    <div v-if="$store.state.token">
      <p>
        已经登录: 可以查看
        <NuxtLink to="/profile"> profile </NuxtLink>
        页面
      </p>
      <button @click="logout">注销登录</button>
    </div>

    <div v-else>
      <span>还未登录, 请先</span>
      <NuxtLink to="/login">登录</NuxtLink>
    </div>
  </div>
</template>

<script>
const Cookie = process.client ? require("js-cookie") : undefined;

export default {
  asyncData({ store }) {
    console.log(store.state);
  },
  methods: {
    logout() {
      Cookie.remove("token");
      this.$store.commit("setToken", null);
    }
  }
};
</script>
